import React, { FC, ProviderProps, useContext } from "react";
import { useAuthority } from "./index";
import { RouteObject } from "react-router-dom";

type AccessContextType = {
  loading?: boolean;
  routes: RouteObject[];
  menuMap: Record<string, any>;
  menuMapId: Record<string, any>;
  menuList: Array<any>;
};
export const AccessContext = React.createContext<AccessContextType>({});
export interface AccessProviderProps<T = any> {
  value?: T;
  children?: React.ReactNode;
}
const AccessProvider: FC<AccessProviderProps> = (props) => {
  const { value = {}, children } = props;
  const { loading, menuList, routes, menuMap, menuMapId } = useAuthority();

  return (
    <>
      {loading ? (
        <div
          style={{
            overflow: "hidden",
            width: "100%",
            height: "100vh",
            position: "fixed",
            zIndex: 100,
          }}
        />
      ) : null}
      <AccessContext.Provider
        value={{
          ...value,
          menuList,
          routes,
          menuMap,
          menuMapId,
        }}
      >
        {children}
      </AccessContext.Provider>
    </>
  );
};
export default AccessProvider;
export const useAccessContext = () => {
  const value = useContext(AccessContext);
  return value || {};
};
